var code = ''
import {
	showToast
} from "vant";
export const isWxBrowser = () => {
	// 判断是否H5微信环境，true为微信浏览器
	const ua = navigator.userAgent.toLowerCase()
	return ua.match(/MicroMessenger/i) == 'micromessenger' ? true : false
}

export const resolveBlob = (res, fileName) => {
	const aLink = document.createElement('a')
	var blob = new Blob([res])
	var fileName = fileName
	fileName = fileName.replace(/\"/g, '')
	aLink.style.display = 'none'
	aLink.href = URL.createObjectURL(blob)
	aLink.setAttribute('download', fileName) // 设置下载文件名称
	document.body.appendChild(aLink)
	aLink.click()
	document.body.removeChild(aLink);
}

export function getCode() { // 非静默授权，第一次有弹框
	let data = {
		isWx: false,
		code: code
	}
	if (!isWxBrowser()) {
		// showToast('请通过微信打开授权！')
		return data;
	}
	var local = window.location.href;
	// var local =  'https://txh5o.westts.cn/regionserviceuser/#/withdraw'; 
	var appid = 'wx64d13e22fd555473' //公众号里有自己查，不会找的查一下百度
	code = getUrlCode().code // 截取code
	if (code == null || code === '') { // 如果没有code，则去请求
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
			"&redirect_uri=" +
			encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo#wechat_redirect"
		// console.log(code)
	} else {
		data.isWx = true
		data.code = code
		console.log(data, 'code')
		return data; //将code返回
		// 你自己的业务逻辑
	}
}
export function getUrlCode() { // 截取url中的code方法
	var url = location.search
	var theRequest = new Object()
	if (url.indexOf("?") != -1) {
		var str = url.substr(1)
		var strs = str.split("&")
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
		}
	}
	// console.log(theRequest)
	return theRequest
}
//复制文本
export function copyText(text) {
	
	navigator.clipboard.writeText(text).then(() => {
		// alert('复制成功')
	});
	// var element = createElement(text);
	// element.select();
	// element.setSelectionRange(0, element.value.length);
	// document.execCommand('copy');
	// element.remove();
	// alert("已复制到剪切板");
}

//创建临时的输入框元素
function createElement(text) {
	var isRTL = document.documentElement.getAttribute('dir') === 'rtl';
	var element = document.createElement('textarea');
	// 防止在ios中产生缩放效果
	element.style.fontSize = '12pt';
	// 重置盒模型
	element.style.border = '0';
	element.style.padding = '0';
	element.style.margin = '0';
	// 将元素移到屏幕外
	element.style.position = 'absolute';
	element.style[isRTL ? 'right' : 'left'] = '-9999px';
	// 移动元素到页面底部
	let yPosition = window.pageYOffset || document.documentElement.scrollTop;
	element.style.top = `${yPosition}px`;
	//设置元素只读
	element.setAttribute('readonly', '');
	element.value = text;
	document.body.appendChild(element);
	return element;
}